package org.dialMsg.Contrllor;


import org.dialMsg.Pojo.TDialResult;
import org.dialMsg.Pojo.User;
import org.dialMsg.Producer.SendTopic;
import org.dialMsg.Producer.SendTopicBatch;
import org.dialMsg.Producer.SendTopicBatch1;
import org.dialMsg.ResultBean.ResponseBean;
import org.dialMsg.Server.ServerImp.MaulResultsServerImp;
import org.dialMsg.Server.ServerInt.TDialResultInter;
import org.dialMsg.Server.ServerInt.UserServerInter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StopWatch;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * @author DPF
 */
@RestController
@RequestMapping("/api/v1")
public class MySendDialControl {
    @Autowired
    private SendTopic sendTopic;
    @Autowired
    private SendTopicBatch sendTopicBatch;
    @Autowired
    private UserServerInter userServerInter;
    @Qualifier("TDialResultImp")
    @Autowired
    private TDialResultInter tDialResultInter;
    @Autowired
    private SendTopicBatch1 sendTopicBatch1;
    @Autowired
    private MaulResultsServerImp maulResultsServerImp;
    @Autowired
    private MaulResultsServerImp1 maulResultsServerImp1;
    @GetMapping("/send")
    public ResponseEntity<ResponseBean<String>> myStaticMethod(@RequestHeader HttpHeaders headers) {

        HttpHeaders responseHeaders = new HttpHeaders();
        // 直接使用具体的 MIME 类型字符串
        responseHeaders.setContentType(MediaType.APPLICATION_JSON);

        // 同步调用 sendMessage 方法
        String keys = null;
        StopWatch watch = new StopWatch("sendTopic");
        watch.start();
        try {
            keys = sendTopic.sendMessage();
            watch.stop();
            System.out.println("sendTopic总耗时：" + watch.getTotalTimeSeconds() + "秒");
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        if (keys == null) {
            // 处理发送失败的情况
            System.err.println("发送失败");
            return ResponseEntity.ok().headers(responseHeaders).body(ResponseBean.error(500, "发送失败"));
        } else {
            return ResponseEntity.ok().headers(responseHeaders).body(ResponseBean.success(keys));
        }
    }

    @GetMapping("/sendbatch")
    public ResponseEntity<ResponseBean<String>> mySendbatch(@RequestHeader HttpHeaders headers) {

        HttpHeaders responseHeaders = new HttpHeaders();
        // 直接使用具体的 MIME 类型字符串
        responseHeaders.setContentType(MediaType.APPLICATION_JSON);

        // 同步调用 sendMessage 方法
        String keys = null;
        StopWatch watch = new StopWatch("sendTopicBatch");
        watch.start();
        try {
            keys = sendTopicBatch.sendMessageBatch();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        watch.stop();
        System.out.println("sendTopicBatch总耗时：" + watch.getTotalTimeSeconds() + "秒");
        if (keys == null) {
            // 处理发送失败的情况
            System.err.println("发送失败");
            return ResponseEntity.ok().headers(responseHeaders).body(ResponseBean.error(500, "发送失败"));
        } else {
            return ResponseEntity.ok().headers(responseHeaders).body(ResponseBean.success(keys));
        }

    }

    @GetMapping("/sendbatch1")
    public ResponseEntity<ResponseBean<String>> mySendbatch1(@RequestHeader HttpHeaders headers) {

        HttpHeaders responseHeaders = new HttpHeaders();
        // 直接使用具体的 MIME 类型字符串
        responseHeaders.setContentType(MediaType.APPLICATION_JSON);

        // 同步调用 sendMessage 方法
        String keys = null;
        StopWatch watch = new StopWatch("sendTopicBatch");
        watch.start();
        keys = sendTopicBatch1.sendMessageBatch();
        watch.stop();
        System.out.println("sendTopicBatch1总耗时：" + watch.getTotalTimeSeconds() + "秒");
        if (keys == null) {
            // 处理发送失败的情况
            System.err.println("发送失败");
            return ResponseEntity.ok().headers(responseHeaders).body(ResponseBean.error(500, "发送失败"));
        } else {
            return ResponseEntity.ok().headers(responseHeaders).body(ResponseBean.success(keys));
        }

    }

    @GetMapping("/manufact/{count}")
    public ResponseEntity<ResponseBean<String>> myManufact(@PathVariable Integer count, @RequestHeader HttpHeaders headers) {

        HttpHeaders responseHeaders = new HttpHeaders();
        // 直接使用具体的 MIME 类型字符串
        responseHeaders.setContentType(MediaType.APPLICATION_JSON);
        // 同步调用 sendMessage 方法
        maulResultsServerImp.sendFactManualCallMessage(count);
        return ResponseEntity.ok().headers(responseHeaders).body(ResponseBean.success("成功"));
    }

    @GetMapping("/app/wuhan/{count}")
    public ResponseEntity<ResponseBean<String>> testWH(@PathVariable Integer count, @RequestHeader HttpHeaders headers) {
        HttpHeaders responseHeaders = new HttpHeaders();
        responseHeaders.setContentType(MediaType.APPLICATION_JSON);

        CompletableFuture<Integer> futureSuccessCount = sendTopic.sendMaulMessageAsync(count);
        try {
            Integer successCount = futureSuccessCount.get(); // 这里会阻塞，直到结果返回
            if (successCount > 0) {
                return ResponseEntity.ok().headers(responseHeaders).body(ResponseBean.success("消息发送成功: " + successCount + " 条"));
            } else {
                return ResponseEntity.status(500).headers(responseHeaders).body(ResponseBean.error(500, "发送失败: " + successCount + " 条"));
            }
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
            return ResponseEntity.status(500).headers(responseHeaders).body(ResponseBean.error(500, "发送过程中发生异常"));
        }
    }

    @GetMapping("/app/hn/{count}")
    public ResponseEntity<ResponseBean<String>> testHN(@PathVariable Integer count, @RequestHeader HttpHeaders headers) {
        HttpHeaders responseHeaders = new HttpHeaders();
        responseHeaders.setContentType(MediaType.APPLICATION_JSON);

        CompletableFuture<Integer> futureSuccessCount = sendTopic.sendHNMessageAsync(count);
        try {
            Integer successCount = futureSuccessCount.get(); // 这里会阻塞，直到结果返回
            if (successCount > 0) {
                return ResponseEntity.ok().headers(responseHeaders).body(ResponseBean.success("消息发送成功: " + successCount + " 条"));
            } else {
                return ResponseEntity.status(500).headers(responseHeaders).body(ResponseBean.error(500, "发送失败: " + successCount + " 条"));
            }
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
            return ResponseEntity.status(500).headers(responseHeaders).body(ResponseBean.error(500, "发送过程中发生异常"));
        }
    }

    @GetMapping("/app/gjbb/{count}")
    public ResponseEntity<ResponseBean<String>> testGjBb(@PathVariable Integer count, @RequestHeader HttpHeaders headers) {
        HttpHeaders responseHeaders = new HttpHeaders();
        responseHeaders.setContentType(MediaType.APPLICATION_JSON);

        CompletableFuture<Integer> futureSuccessCount = sendTopic.sendGjBbMessageAsync(count);
        try {
            Integer successCount = futureSuccessCount.get(); // 这里会阻塞，直到结果返回
            if (successCount > 0) {
                return ResponseEntity.ok().headers(responseHeaders).body(ResponseBean.success("消息发送成功: " + successCount + " 条"));
            } else {
                return ResponseEntity.status(500).headers(responseHeaders).body(ResponseBean.error(500, "发送失败: " + successCount + " 条"));
            }
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
            return ResponseEntity.status(500).headers(responseHeaders).body(ResponseBean.error(500, "发送过程中发生异常"));
        }
    }

    @GetMapping("/findAll")
    public ResponseBean<List<User>> findAll() {
        List<User> userList = userServerInter.findAll();
        if (userList != null) {
            return ResponseBean.success(userList);
        } else {
            return ResponseBean.error(404, "User not found");
        }
    }

    @GetMapping("/queryById")
    public ResponseBean<User> queryById(@RequestParam Integer id) {
        User user = userServerInter.findUserById(id);
        if (user != null) {
            return ResponseBean.success(user);
        } else {
            return ResponseBean.error(404, "User not found");
        }
    }

    @PostMapping("/addTDialResult")
    public ResponseBean<String> addTDialResult(
            @RequestBody TDialResult tDialResult) {
        int result = tDialResultInter.setTDialResult(tDialResult);
        if (result >= 1) {
            return ResponseBean.success("添加成功:" + result + "条");
        } else {
            return ResponseBean.error(404, "添加失败");
        }
    }

}